
##Function to Recode 0 to 1
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

### openness
openness <- mapply(as.numeric,with(wvs_sub,data.frame(
  open1,
  open2,
  open3, 
  open4, 
  open5, 
  open6, 
  open7, 
  open8, 
  open9,
  open10)))

opennesslist <- list()
a <- 0
for(i in 1:9){
  a <- a+(ncol(combn(1:10, i)))
}

b <- 0
items <- list()
for(j in 1:9){
  b=b+1 
  combos <- combn(1:10, j)
  for(i in 1:ncol(combos)){
    items[[b]] <- combos
  }}

itemrow <- lapply(items,nrow)
itemcol <- lapply(items,ncol)

collapsepastecolumns <- function(x)apply(x,2,paste,collapse=",")
rr <- lapply(collapsepastecolumns,X = items)

numbero <-   unlist(itemcol)
emptylist <- list()  
for(i in 2:9){
  emptylist[[i-1]] <- rep(i,numbero[[i-1]])
}

h=1
j=2
i=1
alphacoefs <- data.frame(matrix(nrow=1012,ncol=4))

for(j in 2:9){
  combos <- combn(1:10, j)
  two <- matrix(nrow=nrow(wvs_sub),ncol=ncol(combos))
  for(i in 1:ncol(combos)){
    two[,i] <-   rowMeans(openness[,combos[,i]])
    alphacoefs[h,] <- psych::alpha(openness[,combos[,i]],check.keys = TRUE)$total[1:4]
    h=  h+1
  }
  lmcoefs <- matrix(nrow=ncol(combos),ncol=3)
  for(i in 1:ncol(two)){
    lmcoefs[i,1]   <-   lm(zero1(wvs_sub$wvs_lrplacement)~zero1(two[,i])+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
    lmcoefs[i,2]   <-   lm(zero1(wvs_sub$wvs_moral)~zero1(two[,i])+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
    lmcoefs[i,3]   <-   lm(zero1(wvs_sub$wvs_economicattitudes)~zero1(two[,i])+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
  }
  opennesslist[[j]] <- lmcoefs
  
}
lmcoefs <- matrix(ncol=3)
## 10 item 
lmcoefs[1]   <-   lm(zero1(wvs_sub$wvs_lrplacement)~zero1(rowMeans(openness))+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
lmcoefs[2]   <-   lm(zero1(wvs_sub$wvs_moral)~zero1(rowMeans(openness))+  wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
lmcoefs[3]   <-   lm(zero1(wvs_sub$wvs_economicattitudes)~zero1(rowMeans(openness))+  wvs_sub$IPIP_con+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]

opennesslist[[10]] <- lmcoefs
alphacoefs[1013,] <- psych::alpha(openness,check.keys = TRUE)$total[1:4]


lmcoefs <- matrix(nrow=10,ncol=3)

for(i in 1:ncol(openness)){
  lmcoefs[i,1]   <-   lm(zero1(wvs_sub$wvs_lrplacement)~zero1((openness)[,i])+  wvs_sub$IPIP_con+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
  lmcoefs[i,2]   <-   lm(zero1(wvs_sub$wvs_moral)~zero1((openness)[,i])+  wvs_sub$IPIP_con+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
  lmcoefs[i,3]   <-   lm(zero1(wvs_sub$wvs_economicattitudes)~zero1((openness)[,i])+  wvs_sub$IPIP_con+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
}
opennesslist[[1]] <- lmcoefs

l <- Map(cbind, opennesslist, indx = seq_len(length(opennesslist))) 
h <- data.frame(do.call(rbind, l))
head(h )
names(h) <- c("Left-Right","Moral","Economic Ideology","Index")
empties <- data.frame(matrix(NA,nrow = 10,ncol = 4))
colnames(empties) <- colnames(alphacoefs)
alphacoefs <- rbind(empties,alphacoefs)
itemstogether <- unlist(rr)
itemstogether[1023] <- "all"
numall <- unlist(emptylist)
outstanding <- data.frame(h,itemstogether,alphacoefs)
library(reshape2)
forfig <- melt(outstanding,id.vars=c("Index","itemstogether",names(empties)))
forfig$trait <- "Openness"

